package com.qiku.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qiku.dao.PromotionAdMapper;
import com.qiku.domain.PromotionAd;
import com.qiku.domain.PromotionAdVo;
import com.qiku.service.PromotionAdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class PromotionAdServiceImpl implements PromotionAdService {

    @Autowired
    private PromotionAdMapper promotionAdMapper;

    @Override
    public PageInfo<PromotionAd> findAllAdByPage(Integer currentPage,Integer pageSize) {
        //分页
        PageHelper.startPage(currentPage,pageSize);
        //查询所有
        List<PromotionAd> promotionAdList = promotionAdMapper.findAllAdByPage();
        //封装成PageInfo的时候会自动带上页码、页大小、总数等。
        PageInfo<PromotionAd> pageInfo = new PageInfo<>(promotionAdList);
        System.out.println("adPageInfo:"+ pageInfo);
        return pageInfo;
    }

    @Override
    public int savePromotionAd(PromotionAd promotionAd) {
        Date date = new Date();
        promotionAd.setCreateTime(date);
        promotionAd.setUpdateTime(date);
        return promotionAdMapper.savePromotionAd(promotionAd);
    }

    @Override
    public int updatePromotionAd(PromotionAd promotionAd) {
        Date date = new Date();
        promotionAd.setUpdateTime(date);
        return promotionAdMapper.updatePromotionAd(promotionAd);
    }

    @Override
    public PromotionAd findPromotionAdById(Integer id) {
        return promotionAdMapper.findPromotionAdById(id);
    }

    @Override
    public int updatePromotionAdStatus(Integer id, int status) {
        PromotionAd promotionAd = new PromotionAd();
        Date date = new Date();
        promotionAd.setStatus(status);
        promotionAd.setId(id);
        promotionAd.setUpdateTime(date);
        return promotionAdMapper.updatePromotionAdStatus(promotionAd);
    }
}
